package cn.fanqinghong.system.service.impl;

import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import cn.fanqinghong.system.domain.SystemDictionaryDetail;
import cn.fanqinghong.system.domain.SystemDictionaryType;
import cn.fanqinghong.system.mapper.SystemDictionaryDetailMapper;
import cn.fanqinghong.system.mapper.SystemDictionaryTypeMapper;
import cn.fanqinghong.system.query.SystemDictionaryDetailQuery;
import cn.fanqinghong.system.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * FAN
 * 2022/6/8  16:23
 */
@Service
public class SystemDictionaryTypeServiceImpl extends BaseServiceImpl<SystemDictionaryType> implements ISystemDictionaryTypeService {
    @Autowired
    private SystemDictionaryTypeMapper systemDictionaryTypeMapper;
    @Autowired
    private SystemDictionaryDetailMapper systemDictionaryDetailMapper;

    /**
     * 删除一条数据时要同时删除副表的数据
     * @param id
     */
    @Override
    public void removeById(Long id) {
        //通过id查询到查询所有数据
        List<SystemDictionaryDetail> details = systemDictionaryDetailMapper.queryByTypeId(id);
        //设置一个空数组用来存取查询到的数据中的id
        List<Long> ids = new ArrayList<>();
        //遍历查询到的数组
        for (SystemDictionaryDetail detail : details) {
            //获取到数组中的id值
            Long tid = detail.getId();
            //把id添加到空数组中
            ids.add(tid);
        }
        //判断新数组是否为空
        if (ids.size()!=0){
            //不为空就删除副标中的数据
            systemDictionaryDetailMapper.batch(ids);
        }
        //删除主表中的数据
        systemDictionaryTypeMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batch(List<Long> ids) {
        for (Long id : ids) {
            this.removeById(id);
        }
    }


}
